/**
 * @file zuoUI.c
 * @author 左~~
 * @brief 用于UI框架展示
 * @version 4.5
 * @date 2025-5-16
 *
 *
 * @copyright Copyright (c) 2024
 *
 */
/*
* 重大更新日志（修复BUG不在此列）
 1.0 十字键框架
 1.2 解耦部分静态控件绘制
 1.3 更改颜色设置机制
 1.5 移除了行号选择器
 2.0 增加了固定判定箱触发器，输入方式改为触摸
 2.3 增加了判定箱注册表
 2.4 解耦全部控件绘制器
 3.0 更改为函数式页面管理
 3.1 新增回调函数数据出口
 3.5 新增集成控件 按钮
 4.0 在触发器中新增事件处理，局部刷新
 4.1 新增控件 文本框 标签
 4.2 新增控件数字调整条
 4.4 独立事件注册表

* 截至2025年5月中，该框架尚未完全，尤其在于动态内容的刷新上
* 在申明头中 存在一个变量，用于四对数据的交互

*/

#include "zuoUI.h"
#include "delay.h"

// #include "zuooled.h"

#include "../HARDWARE/LCD/LCD.h"

u16 EventList[20] = {0}; // 事件句柄,页面ID,该值为0时无效
u16 TrigList[10][4];                                // 菜单按钮触发器句柄,判定箱的左上和右下坐标(X1,Y1,X2,Y2)
u8 EventNum = 0;                                    // 当前页面注册控件数,每次绘制页面时都会刷新

u8 SwtichList[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 局部控件触发器类标志位 0 无 1 numericUpDown
u16 SwitchTrigList[20][4];                                                        // 局部控件触发器句柄,判定箱的左上和右下坐标(X1,Y1,X2,Y2)
u8 SwtichTag[20];                                                                 // 局部控件触发器附加标签,一般用于绑定变量
u8 SwtichTag2[20];                                                                // 局部控件触发器附加标签,一般用于表示行为 0X 减 1X 加
u8 SwtichNum = 0;                                                                 // 页面注册的局部控件数

u16 testNum[10]; // 控件测试

u16 UI_PageNum = 0; // 当前页码

u16 UI_DataBank1[6][20];

/// @brief UI 交互数据接口 实际值
/// @attention 该接口5.0时废弃
unsigned long UI_Data[9] = {255, 253, 251, 252, 265, 273, 290, 259,300};
u16 UI_POWData[4] = {50,50,50,50}; //UI临时数据接口，功率百分比计算，按照温度计算
u8 showing = 0 ;//热堆积算法演示模式

// 定时刷新定向标志
u16 EventTell = 0;
extern int isDeviceOnline;  

u16 pagenow = 0; //当前页面

/*******
 * @brief API
 *
 */

/// @brief 绘制一个按钮
/// @param X1 控件横坐标
/// @param Y1 控件纵坐标
/// @param str 文本
/// @param Button 按键结构体实例
void UI_Draw_Button(u16 X1, u16 Y1, unsigned char str[], UI_Button Button)
{
    // 注册事件
    EventList[EventNum]   = Button.ID;
    TrigList[EventNum][0] = X1;
    TrigList[EventNum][1] = Y1;
    TrigList[EventNum][2] = X1 + Button.Width;
    TrigList[EventNum][3] = Y1 + Button.Heigth;
    EventNum++;
    POINT_COLOR = Button.fontColor;
    BACK_COLOR  = Button.backColor;
    LCD_Color_Fill(X1, Y1, Button.Width + X1, Button.Heigth + Y1, Button.backColor);
    // LCD_DrawRectangle(X1, Y1, Button.Width + X1, Button.Heigth + Y1);
    LCD_ShowString(X1 + Button.side, Y1 + (Button.Heigth / 3), Button.Width, Button.Heigth / 2, Button.fontSize, str);
}

/// @brief 绘制一个文本
/// @param X 起始横坐标
/// @param Y 起始纵坐标
/// @param str 文本
/// @param label 只读文本框结构体实例
void UI_Draw_Label(u16 X, u16 Y, u8 str[], UI_Label label)
{
    POINT_COLOR = label.fontColor;
    BACK_COLOR  = label.backColor;
    LCD_Color_Fill(X, Y, label.Width + X, label.Heigth + Y, label.backColor);
    LCD_ShowString(X + label.side, Y + (label.Heigth / 3), label.Width, label.Heigth / 2, label.fontSize, str);
}

/// @brief 绘制一个数字条
/// @param X 绘制横坐标
/// @param Y 绘制纵坐标
/// @param ID 控件绑定变量索引
/// @attention 控件白色区域为动态刷新区域 (X+60,Y+5,X+140,Y+40)
void UI_Draw_NumericUpDown(u16 X, u16 Y, u8 ID)
{
    // 注册增加,减小按钮
    SwtichList[SwtichNum]        = SwtichNum + 1;
    SwtichTag[SwtichNum]         = ID;
    SwtichTag2[SwtichNum]        = 0x01;
    SwitchTrigList[SwtichNum][0] = X;
    SwitchTrigList[SwtichNum][1] = Y;
    SwitchTrigList[SwtichNum][2] = X + 60;
    SwitchTrigList[SwtichNum][3] = Y + 40;
    SwtichNum++;
    SwtichList[SwtichNum]        = SwtichNum + 1;
    SwtichTag[SwtichNum]         = ID;
    SwtichTag2[SwtichNum]        = 0x11;
    SwitchTrigList[SwtichNum][0] = X + 140;
    SwitchTrigList[SwtichNum][1] = Y;
    SwitchTrigList[SwtichNum][2] = X + 200;
    SwitchTrigList[SwtichNum][3] = Y + 40;
    SwtichNum++;

    BACK_COLOR  = 0x6B4D;
    POINT_COLOR = BLACK;
    LCD_Color_Fill(X, Y, X + 60, Y + 40, 0x6B4D);
    BACK_COLOR = WHITE;
    LCD_Color_Fill(X + 60, Y, X + 140, Y + 40, WHITE);
    BACK_COLOR = 0xF80F;
    LCD_Color_Fill(X + 140, Y, X + 200, Y + 40, 0xF80F);
    LCD_ShowChar(X + 20, Y + 5, '-', 32, 1);
    POINT_COLOR = 0xFFFF;
    LCD_ShowChar(X + 160, Y + 5, '+', 32, 1);
    POINT_COLOR = 0x0;
    LCD_DrawRectangle(X, Y, X + 201, Y + 41);
    POINT_COLOR = BLACK;
    BACK_COLOR  = WHITE;
    // LCD_ShowString(X+63,Y,143,40,32,"31.0");
}

/***
 * @brief 控件事件与局部刷新 （未启用）
 * 每个页面对应一个重绘函数,传入触发控件序号
 * 如要使用局部刷新:
 * 1.确保页面静态已经绘制
 * 2.注册触发器与绘制事件
 * 3.当一个页面启用了动态绘制,那么在切换页之前,不会再次绘制静态样式表,而是进入此区域
 * 切换页优先于此
 * @attention 新增局部绘制方法需要在主捕获函数中注册函数
 * @attention 切换页时,所有标志位将清空
 */

/**
 * 使用 extern 绑定外部数据
 * 使用 UI_DATA 访问 UI驱动的数据
 *
 */

unsigned int timeCount = 0x09F;
unsigned int tempCount = 0;
u8 signCount = 0;
unsigned long temp[] = {280,299,303,312,318,320,325,329,331};
unsigned char temp2[] ={ 10, 10, 8, 7,   6  , 6, 5,  5,  4};
 /// @brief 演示热堆积算法
/// ID: 0xBBB
void UI_Active_BackGround()
{
    if(timeCount)
        {timeCount--;return;}
    //倒数执行结束，开始升温
    if(tempCount)
        {tempCount--;return;}
    tempCount = 0x3F;
    
    UI_Data[0]=temp[signCount++];
    UI_POWData[0]=temp2[signCount];
    //UI_Data[0] = 671;
    if(signCount>=7)
        signCount = 7;
}

/// @brief 刷新0xB20所属动态部分
/// ID: 0xB20
void UI_Active_DataSheet()
{
    BACK_COLOR = GREEN;
    // 此处存在一个问题 UI_DATA[0] 读出的值为零
    LCD_ShowFlaot(250, 185, UI_Data[0], 3, 24);
    LCD_ShowFlaot(250, 305, UI_Data[1], 3, 24);
    LCD_ShowFlaot(250, 445, UI_Data[2], 3, 24);
    LCD_ShowFlaot(250, 585, UI_Data[3], 3, 24);
    // LCD_ShowString(X+63,Y,143,40,32,"31.0");
    LCD_ShowFlaot(73, 185, UI_Data[4], 3, 24);
    LCD_ShowFlaot(73, 305, UI_Data[5], 3, 24);
    LCD_ShowFlaot(73, 445, UI_Data[6], 3, 24);
    LCD_ShowFlaot(73, 585, UI_Data[7], 3, 24);
    



    //核算加热功率
    ///具体计算方法为距离标准温度的差值比上标准值
    ///若温度大于等于设定值，则强制输出0
    int _temp = 0;
    u8 i = 0;
    for (i = showing ? 1 : 0; i < 4; i++)
    {
        _temp =   UI_Data[i+4]- UI_Data[i] ;
        if(UI_Data[i] < UI_Data[i+4])
            UI_POWData[i] = (int)((_temp));
        else
            UI_POWData[i] = 0;
            
        if(UI_POWData[i]<10)
            UI_POWData[i] = 10;
        if(UI_POWData[i]>100)
            UI_POWData[i] = 100;

    }
    // if(showing)
    // {
    //     UI_Active_BackGround();
    // }
     
  
    LCD_ShowNum(400, 185, UI_POWData[0], 3, 32);
    LCD_ShowNum(400, 305, UI_POWData[1], 3, 32);
    LCD_ShowNum(400, 445, UI_POWData[2], 3, 32);
    LCD_ShowNum(400, 585, UI_POWData[3], 3, 32);
    


    
    //UI_POWData[i]
}


/// @brief 从机列表
/// ID: 0xB40
void UI_Active_DeviceList()
{
    POINT_COLOR = 0x6B4D;
    //LCD_ShowString(250, 200, 240, 24, 24, "online");
    if((isDeviceOnline & 0x1))
     {showhanzi(250, 100,63);showhanzi(290, 100,64);}
    if((isDeviceOnline & 0x2))
     {showhanzi(250, 150,63);showhanzi(290, 150,64);}
    if((isDeviceOnline & 0x4))
     {showhanzi(250, 200,63);showhanzi(290, 200,64);}
    if((isDeviceOnline & 0x8))
     {showhanzi(250, 250,63);showhanzi(290, 250,64);}  
}
/***
 * @brief 页静态面样式表
 * 一个页面的标准流程为:
 * 1.销毁事件列表
 * 2.清空当前显示
 * 3.绘制样式
 * 4.注册事件与触发器
 * 5.交还控制权
 * @attention 需要在主捕获函数中注册函数
 * @attention 对于被驱动需要更新的页面,不推荐重新绘制整个页面
 */

/// @brief 添加设备页,
/// @brief ID:0xB10
void UI_Page_AddNew()
{
     for (u8 i = 0; i < 20; i++) // 已经进入事件的话就销毁所有挂起事件
        {
            //temp[i]      = EventList[i];
            EventList[i] = 0;
        }
    LCD_Fill(0, 100, 480, 800, WHITE);
    UI_Label mylabel;
    mylabel.Heigth    = 100;
    mylabel.Width     = 480;
    mylabel.backColor = 0x7B5D;
    mylabel.fontColor = WHITE;
    mylabel.side      = 10;
    mylabel.fontSize  = 32;
    UI_Draw_Label(0, 0, "", mylabel);
    showhanzi(10, 30, 0);
    showhanzi(42, 30, 1);
    showhanzi(74, 30, 2);
    showhanzi(106, 30, 3);
    showhanzi(138, 30, 4);
    BACK_COLOR  = WHITE;
    POINT_COLOR = BLACK;
    // LCD_ShowString(5, 150, 240, 40, 24, "Adding Mode:");
    showhanzi(10, 150, 0);
    showhanzi(42, 150, 1);
    showhanzi(74, 150, 20);
    showhanzi(106, 150, 21);
    // LCD_ShowString(5, 300, 240, 40, 24, "Next Device ID:");
    showhanzi(10, 300, 22);
    showhanzi(42, 300, 23);
    showhanzi(74, 300, 24);
    showhanzi(106, 300, 25);
    showhanzi(138, 300, 26);
    showhanzi(170, 300, 27);
    showhanzi(202, 300, 28);
    showhanzi(234, 300, 29);

    // LCD_ShowString(5, 450, 240, 40, 24, "Next Group ID:");
    showhanzi(10, 450, 22);
    showhanzi(42, 450, 23);
    showhanzi(74, 450, 24);
    showhanzi(106, 450, 25);
    showhanzi(138, 450, 26);
    showhanzi(170, 450, 27);
    showhanzi(202, 450, 30);
    // BACK_COLOR=0x75C7;
    BACK_COLOR = 0x7E0;
    // LCD_ShowString(200, 220, 240, 40, 32, "AUTO");
    showhanzi(200, 220, 59);
    showhanzi(232, 220, 60);
    BACK_COLOR  = WHITE;
    POINT_COLOR = 0x6B4D;
    // LCD_ShowString(200, 370, 240, 40, 32, "AUTO (0x05)");
    showhanzi(200, 370, 59);
    showhanzi(232, 370, 60);
    // LCD_ShowString(200, 520, 240, 40, 32, "AUTO (0x02)");
    showhanzi(200, 520, 59);
    showhanzi(232, 520, 60);
    UI_Button mybutton;
    mybutton.backColor = YELLOW;
    mybutton.fontColor = BLUE;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB01;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}


/// @brief 数据总览页
/// ID:0xB20
void UI_Page_DataSheet()
{
     for (u8 i = 0; i < 20; i++) // 已经进入事件的话就销毁所有挂起事件
        {
            //temp[i]      = EventList[i];
            EventList[i] = 0;
        }
    LCD_Fill(0, 100, 480, 800, WHITE);
    SwtichNum = 0;
    UI_Label mylabel;
    mylabel.Heigth    = 100;
    mylabel.Width     = 480;
    mylabel.backColor = 0x7B5D;
    mylabel.fontColor = WHITE;
    mylabel.side      = 10;
    mylabel.fontSize  = 32;
    UI_Draw_Label(0, 0, "", mylabel);
    showhanzi(10, 30, 5);
    showhanzi(42, 30, 6);
    showhanzi(74, 30, 7);
    showhanzi(106, 30, 8);
    showhanzi(138, 30, 9);

    showing = 0 ;//关闭演示模式
    signCount = 0;
    UI_Data[0] =255;

   // LCD_Fill(240, 120, 240 + 150, 120 + 40, 0xFE19);
    BACK_COLOR  = 0xFE19;
    POINT_COLOR = BLACK;
    // LCD_ShowString(250,125,150,40,24,"28.3");
    //  LCD_ShowFlaot(250,125,UI_Data[0],3,24);
    UI_Draw_NumericUpDown(10, 180, 1);
    BACK_COLOR = WHITE;
    LCD_ShowString(80, 110, 400, 40, 32, " 1 ");
    showhanzi(10, 110, 61);
    showhanzi(42, 110, 62);

    showhanzi(250, 110, 68);
    showhanzi(282, 110, 69);

    showhanzi(380, 110, 70);
    showhanzi(412, 110, 71);
    showhanzi(444, 110, 72);

    BACK_COLOR = WHITE;
    LCD_ShowString(80, 230, 400, 40, 32, " 2");
    showhanzi(10, 230, 61);
    showhanzi(42, 230, 62);
   // LCD_Fill(240, 240, 240 + 150, 240 + 40, 0xFE19);
    BACK_COLOR = 0xFE19;
    // LCD_ShowString(250,245,150,40,24,"29.3");
    //LCD_ShowFlaot(250, 245, UI_Data[1], 3, 24);
    UI_Draw_NumericUpDown(10, 300, 2);

    BACK_COLOR = WHITE;
    LCD_ShowString(80, 370, 400, 40, 32, " 3");
    showhanzi(10, 370, 61);
    showhanzi(42, 370, 62);
   // LCD_Fill(240, 380, 240 + 150, 380 + 40, 0xFE19);
    BACK_COLOR = 0xFE19;
    // LCD_ShowString(250,385,150,40,24,"27.9");
    //LCD_ShowFlaot(250, 385, UI_Data[2], 3, 24);
    UI_Draw_NumericUpDown(10, 440, 3);

    BACK_COLOR = WHITE;
    LCD_ShowString(80, 510, 400, 40, 32, " 4");
    showhanzi(10, 510, 61);
    showhanzi(42, 510, 62);
    //LCD_Fill(240, 520, 240 + 150, 520 + 40, 0xFE19);
    BACK_COLOR = 0xFE19;
    // LCD_ShowString(250,525,150,40,24,"29.5");
    //LCD_ShowFlaot(250, 525, UI_Data[3], 3, 24);
    UI_Draw_NumericUpDown(10, 580, 4);

    // 挂起定时刷新
    EventTell = 0xB20;
    // SwtichList[SwtichNum]        = SwtichNum + 1;
    // SwtichTag[SwtichNum]         = 9;
    // SwtichTag2[SwtichNum]        = 0x11;
    // SwitchTrigList[SwtichNum][0] = 0;
    // SwitchTrigList[SwtichNum][1] = 0;
    // SwitchTrigList[SwtichNum][2] = 200 ;
    // SwitchTrigList[SwtichNum][3] = 100;

    UI_Button mybutton;
    mybutton.backColor = YELLOW;
    mybutton.fontColor = BLUE;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB01;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}

/// @brief wifi设置界面
/// ID:0xB30
void UI_Page_WifiSetting()
{
     for (u8 i = 0; i < 20; i++) // 已经进入事件的话就销毁所有挂起事件
        {
            //temp[i]      = EventList[i];
            EventList[i] = 0;
        }
    LCD_Fill(0, 100, 480, 800, WHITE);
    UI_Label mylabel;
    mylabel.Heigth    = 100;
    mylabel.Width     = 480;
    mylabel.backColor = 0x7B5D;
    mylabel.fontColor = WHITE;
    mylabel.side      = 10;
    mylabel.fontSize  = 32;
    UI_Draw_Label(0, 0, "", mylabel);
    showhanzi(10, 30, 10);
    showhanzi(42, 30, 11);
    showhanzi(74, 30, 12);
    showhanzi(106, 30, 13);
    showhanzi(138, 30, 14);
    showhanzi(170, 30, 15);

    BACK_COLOR  = WHITE;
    POINT_COLOR = BLACK;
    // LCD_ShowString(5, 150, 240, 40, 24, "SSID");
    showhanzi(10, 150, 10);
    showhanzi(42, 150, 11);
    showhanzi(74, 150, 37);
    showhanzi(106, 150, 38);
    showhanzi(138, 150, 39);
    showhanzi(170, 150, 40);
    // LCD_ShowString(5, 300, 240, 40, 24, "PassWord");
    showhanzi(10, 300, 37);
    showhanzi(42, 300, 38);
    showhanzi(74, 300, 41);
    showhanzi(106, 300, 42);
    // LCD_ShowString(5, 450, 240, 40, 24, "Request Time");
    showhanzi(10, 450, 43);
    showhanzi(42, 450, 44);
    showhanzi(74, 450, 45);
    showhanzi(106, 450, 46);
    // BACK_COLOR=0x75C7;
    LCD_ShowString(200, 220, 240, 40, 32, "red");
    LCD_ShowString(200, 370, 240, 40, 32, "12344321");
    LCD_ShowString(200, 520, 240, 40, 32, "3 S");
    POINT_COLOR = BLUE;
    LCD_DrawRectangle(50, 218, 479, 252);
    LCD_DrawRectangle(49, 217, 480, 253);
    LCD_DrawRectangle(50, 372, 479, 406);
    LCD_DrawRectangle(49, 371, 480, 407);
    LCD_DrawRectangle(50, 520, 479, 560);
    LCD_DrawRectangle(50, 519, 479, 559);
    POINT_COLOR = GREEN;
    // LCD_ShowString(5, 600, 240, 40, 24, "WIFI Connect");
    showhanzi(10, 600, 12);
    showhanzi(42, 600, 13);
    showhanzi(74, 600, 47);
    showhanzi(106, 600, 48);
    UI_Button mybutton;
    mybutton.backColor = YELLOW;
    mybutton.fontColor = BLUE;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB01;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}

/// @brief 从机列表
/// ID: 0xB40
void UI_Page_DeviceList()
{
     for (u8 i = 0; i < 20; i++) // 已经进入事件的话就销毁所有挂起事件
        {
            //temp[i]      = EventList[i];
            EventList[i] = 0;
        }
    LCD_Fill(0, 100, 480, 800, WHITE);
    UI_Label mylabel;
    mylabel.Heigth    = 100;
    mylabel.Width     = 480;
    mylabel.backColor = 0x7B5D;
    mylabel.fontColor = WHITE;
    mylabel.side      = 10;
    mylabel.fontSize  = 32;
    UI_Draw_Label(0, 0, "", mylabel);
    showhanzi(10, 30, 3);
    showhanzi(42, 30, 4);
    showhanzi(74, 30, 16);
    showhanzi(106, 30, 17);

    BACK_COLOR  = WHITE;
    POINT_COLOR = BLACK;
    // LCD_ShowString(0,200,240,24,24,"ROOM1 CH1");
    showhanzi(10, 100, 61);
    showhanzi(42, 100, 62);
    LCD_ShowString(80, 100, 400, 40, 32, " 1 ");
   
    showhanzi(10, 150, 61);
    showhanzi(42, 150, 62);
    LCD_ShowString(80, 150, 400, 40, 32, " 2 ");
    showhanzi(10, 200, 61);
    showhanzi(42, 200, 62);
    LCD_ShowString(80, 200, 400, 40, 32, " 3 ");
    showhanzi(10, 250, 61);
    showhanzi(42, 250, 62);
    LCD_ShowString(80, 250, 400, 40, 32, " 4 ");

    // LCD_ShowString(0,250,240,24,24,"ROOM1 CH2");
    // LCD_ShowString(0,300,240,24,24,"ROOM1 CH3");
    // LCD_ShowString(0,350,240,24,24,"ROOM2 CH1");

    POINT_COLOR = 0x6B4D;
    //LCD_ShowString(250, 200, 240, 24, 24, "online");
    //if(isDeviceOnline & 0x1)
     {showhanzi(250, 100,63);showhanzi(290, 100,64);}
    //if(isDeviceOnline & 0x2)
     {showhanzi(250, 150,63);showhanzi(290, 150,64);}
    //if(isDeviceOnline & 0x4)
     {showhanzi(250, 200,63);showhanzi(290, 200,64);}
    //if(isDeviceOnline & 0x8)
     {showhanzi(250, 250,63);showhanzi(290, 250,64);}
    //LCD_ShowString(250, 250, 240, 24, 24, "online");
    //LCD_ShowString(250, 300, 240, 24, 24, "online");
    //LCD_ShowString(250, 350, 240, 24, 24, "online");
    //EventTell = 0xB40;

    UI_Button mybutton;
    mybutton.backColor = YELLOW;
    mybutton.fontColor = BLUE;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB01;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}
/// @brief 其他选项页
/// ID:0xB50
void UI_Page_Orther()
{
     for (u8 i = 0; i < 20; i++) // 已经进入事件的话就销毁所有挂起事件
        {
            //temp[i]      = EventList[i];
            EventList[i] = 0;
        }
    LCD_Fill(0, 0, 480, 800, WHITE);
    UI_Button mybutton;
    mybutton.backColor = 0x07F0;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 24;
    mybutton.Width     = 300;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB51;
    mybutton.side      = 20;
    UI_Draw_Button(0, 50, " ", mybutton);
    showhanzi(10, 70, 49);
    showhanzi(42, 70, 50);
    showhanzi(74, 70, 14);
    showhanzi(106, 70, 15);

    mybutton.backColor = 0xF014;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 24;
    mybutton.Width     = 300;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB52;
    mybutton.side      = 40;
    UI_Draw_Button(0, 200,  " ", mybutton);
    showhanzi(10, 230, 51);
    showhanzi(42, 230, 52);
    showhanzi(74, 230, 53);
    showhanzi(106, 230, 54);

    mybutton.backColor = 0xFC13;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 24;
    mybutton.Width     = 300;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB53;
    mybutton.side      = 30;
    UI_Draw_Button(0, 350, "", mybutton);
    showhanzi(10, 380, 55);
    showhanzi(42, 380, 56);
    showhanzi(74, 380, 57);
    showhanzi(106, 380, 58);

    mybutton.backColor = YELLOW;
    mybutton.fontColor = BLUE;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB01;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}

/// @brief 总线通信配置页面
/// ID: 0xB51
void UI_Page_LineSetting()
{

    LCD_Fill(0, 100, 480, 800, WHITE);
    UI_Label mylabel;
    mylabel.Heigth    = 100;
    mylabel.Width     = 480;
    mylabel.backColor = 0x7B5D;
    mylabel.fontColor = WHITE;
    mylabel.side      = 10;
    mylabel.fontSize  = 32;
    UI_Draw_Label(0, 0, "", mylabel);
    showhanzi(10, 30, 49);
    showhanzi(42, 30, 50);
    showhanzi(74, 30, 14);
    showhanzi(106, 30, 15);

    BACK_COLOR  = WHITE;
    POINT_COLOR = BLACK;
   // LCD_ShowString(5, 150, 240, 40, 24, "Transport Seeped");
     showhanzi(10, 150, 12);
    showhanzi(42, 150, 13);
    showhanzi(74, 150, 66);
    showhanzi(106, 150, 67);
   // LCD_ShowString(5, 300, 240, 40, 24, "DataBag Head");
    showhanzi(10, 300, 5);
    showhanzi(42, 300, 6);
    showhanzi(74, 300, 65);
    LCD_ShowString(5, 450, 240, 40, 24, "");
    showhanzi(10, 450, 43);
    showhanzi(42, 450, 44);
    showhanzi(74, 450, 45);
    showhanzi(106, 450, 46);
    // BACK_COLOR=0x75C7;
    LCD_ShowString(200, 220, 240, 40, 32, "115200 Bit/s");
    LCD_ShowString(200, 370, 240, 40, 32, "0xFF");
    LCD_ShowString(200, 520, 240, 40, 32, "3 S");
    POINT_COLOR = BLUE;
    LCD_DrawRectangle(50, 218, 479, 251);
    LCD_DrawRectangle(49, 217, 480, 252);
    LCD_DrawRectangle(50, 372, 479, 405);
    LCD_DrawRectangle(49, 371, 480, 406);
    LCD_DrawRectangle(50, 520, 479, 559);
    LCD_DrawRectangle(50, 519, 479, 558);

    UI_Button mybutton;
    mybutton.backColor = 0x7FE0;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB50;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}

/// @brief LCD配置与测试
/// ID: 0xB52
void UI_Page_LCD_pic()
{
    LCD_Fill(0, 0, 480, 800, WHITE);
    LCD_ShowPic(0, 0);
    UI_Button mybutton;
    mybutton.backColor = 0x7FE0;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB50;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}

/// @brief 系统信息页
/// ID:0xB53
void UI_Page_SystemInfo()
{
    LCD_Fill(0, 100, 480, 800, WHITE);
    UI_Label mylabel;
    mylabel.Heigth    = 100;
    mylabel.Width     = 480;
    mylabel.backColor = 0x7B5D;
    mylabel.fontColor = WHITE;
    mylabel.side      = 10;
    mylabel.fontSize  = 32;
    UI_Draw_Label(0, 0, "System Information", mylabel);
    BACK_COLOR  = WHITE;
    POINT_COLOR = BLACK;
    LCD_ShowString(0, 200, 240, 24, 24, "Center Program Unit");
    LCD_ShowString(0, 250, 240, 24, 24, "Program Version");
    LCD_ShowString(0, 300, 240, 24, 24, "Device ID");
    LCD_ShowString(0, 350, 240, 24, 24, "MAX Conncet Num");
    LCD_ShowString(0, 400, 240, 24, 24, "Program Coding Time");
    LCD_ShowString(0, 450, 240, 24, 24, "Internet name");
    LCD_ShowString(0, 500, 240, 24, 24, "Internet passwor");
    LCD_ShowString(0, 550, 240, 24, 24, "System Active");
    LCD_ShowString(0, 600, 240, 24, 24, "Active Time");
    LCD_ShowString(0, 650, 240, 24, 24, "Lisence Type");
    POINT_COLOR = 0x6B4D;
    LCD_ShowString(250, 200, 240, 24, 24, "STM32F429IGT6");
    LCD_ShowString(250, 250, 240, 24, 24, "V4.3");
    LCD_ShowString(250, 300, 240, 24, 24, "3DF3DC0AB3E1C");
    LCD_ShowString(250, 350, 240, 24, 24, "65535(16bit)");
    LCD_ShowString(250, 400, 240, 24, 24, "2025/4/23");
    LCD_ShowString(250, 450, 240, 24, 24, "STM32F4_LINK");
    LCD_ShowString(250, 500, 240, 24, 24, "**************");
    LCD_ShowString(250, 550, 240, 24, 24, "ACTIVED");
    LCD_ShowString(250, 600, 240, 24, 24, "2025/4/23");
    LCD_ShowString(250, 650, 240, 24, 24, "ADMIN");

    UI_Button mybutton;
    mybutton.backColor = 0x7FE0;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 32;
    mybutton.Width     = 200;
    mybutton.Heigth    = 100;
    mybutton.ID        = 0xB50;
    mybutton.side      = 40;
    UI_Draw_Button(280, 700, "", mybutton);
    showhanzi(320, 750, 31);
    showhanzi(352, 750, 32);
}

/// @brief 起始HOME页
/// ID:0xB01
void UI_Page_Start()
{
    // LCD_Fill(0, 0, 480, 800, WHITE);

    UI_Button mybutton;
    mybutton.backColor = 0xfec6;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 32;
    mybutton.Width     = 479;
    mybutton.Heigth    = 168;
    mybutton.ID        = 0xB10;
    mybutton.side      = 10;
    UI_Draw_Button(0, 0, "", mybutton);
    showhanzi(10, 84, 0);
    showhanzi(42, 84, 1);
    showhanzi(74, 84, 2);
    showhanzi(106, 84, 3);
    showhanzi(138, 84, 4);
    delay_ms(50);
    mybutton.backColor = 0xFF47;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 32;
    mybutton.Width     = 479;
    mybutton.Heigth    = 168;
    mybutton.ID        = 0xB20;
    mybutton.side      = 10;
    UI_Draw_Button(0, 168, "", mybutton);
    showhanzi(10, 252, 5);
    showhanzi(42, 252, 6);
    showhanzi(74, 252, 7);
    showhanzi(106, 252, 8);
    showhanzi(138, 252, 9);
    delay_ms(50);
    mybutton.backColor = 0xFF6B;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 32;
    mybutton.Width     = 479;
    mybutton.Heigth    = 168;
    mybutton.ID        = 0xB30;
    mybutton.side      = 10;
    UI_Draw_Button(0, 336, "", mybutton);
    showhanzi(10, 420, 10);
    showhanzi(42, 420, 11);
    showhanzi(74, 420, 12);
    showhanzi(106, 420, 13);
    showhanzi(138, 420, 14);
    showhanzi(170, 420, 15);
    delay_ms(50);
    mybutton.backColor = 0xFF8E;
    mybutton.fontColor = BLACK;
    mybutton.fontSize  = 32;
    mybutton.Width     = 479;
    mybutton.Heigth    = 168;
    mybutton.ID        = 0xB40;
    mybutton.side      = 10;
    UI_Draw_Button(0, 504, "", mybutton);
    showhanzi(10, 588, 3);
    showhanzi(42, 588, 4);
    showhanzi(74, 588, 16);
    showhanzi(106, 588, 17);
    //delay_ms(50);
    // mybutton.backColor = 0xFFB3;
    // mybutton.fontColor = BLACK;
    // mybutton.fontSize  = 32;
    // mybutton.Width     = 479;
    // mybutton.Heigth    = 168;
    // mybutton.ID        = 0xB50;
    // mybutton.side      = 10;
    // UI_Draw_Button(0, 672, "", mybutton);
    // showhanzi(10, 736, 18);
    // showhanzi(42, 736, 19);
    // showhanzi(74, 736, 14);
    // showhanzi(106, 736, 15);

    // UI_Button mybutton;
}

/// @brief 开屏绘制
void UI_index()
{
    EventList[0]= 1;
    u8 i = 0;
    LCD_Fill(0, 0, 480, 800, WHITE);
    
    while (i++ < 0x1e) {
        LCD_Fill(0, 0, 480, 800, 0x7e0 + i);
        delay_us(1500);
    }
    while (--i) {
        LCD_Fill(0, 0, 480, 800, 0x7e0 + i);
        delay_us(1500);
    }
    while (i++ < 0x1e) {
        LCD_Fill(0, 0, 480, 800, 0x7e0 + i);
        delay_us(1500);
    }

    // LCD_Fill(0,0,480,800,WHITE);
    UI_Page_Start();
}
/***
 * @brief 事件处理函数与触发器
 */
// H->L  已经触发过 按下中
u8 _signed = 0x00; // 系统状态标志位聚合

/// @brief 局部控件的变量操作函数
/// @param num 控件序号
/// @todo 注意：方法中的switch将在5.0后弃用
///
void UI_Handle_Active(u16 num)
{
// num--;

// 临时接口
#if 1
    switch (num) {
        case 1:
            UI_Data[4]--;
            break;
        case 2:
            UI_Data[4]++;
            break;
        case 3:
            UI_Data[5]--;
            break;
        case 4:
            UI_Data[5]++;
            break;
        case 5:
            UI_Data[6]--;
            break;
        case 6:
            UI_Data[6]++;
            break;
        case 7:
            UI_Data[7]--;
            break;
        case 8:
            UI_Data[7]++;
            break;
        case 9:
            showing =1;
            break;

        default:
             UI_Page_Start();
            break;
    }
    return;

#endif

// 5.0 后启用的框架
#if 0
    u16 temp = testNum[num];
    if(SwtichTag2[num] & 0xF0)
        temp += SwtichTag2[num] & 0xF;
    else
        temp -= SwtichTag2[num] & 0xF   ;
    testNum[num] = temp ;
#endif
}

/// @brief 驱动UI主事件捕获
/// @param TouthX 触摸X坐标
/// @param TouthY 触摸Y坐标
/// @param callfunction 事件处理回调函数
void UI_Handle_Main(u16 TouthX, u16 TouthY, void callfunction(u16 ID, u16 num))
{


    u16 pageID   = 0;
    u16 activeID = 0;
    u16 temp[10];

    if (TouthX > 500) // 松手就结束本次触摸
    {
        _signed &= ~0x02;
        return;
    }

    // 触发器判定,如果事件ID为0,那么触发器无效
    for (u8 i = 0; i < 20; i++) {
        if (!EventList[i])
            continue;
        else if (TouthX > TrigList[i][0] && TouthX < TrigList[i][2] && TouthY > TrigList[i][1] && TouthY < TrigList[i][3]) {
            {pageID = EventList[i];if(pageID>0xBB0)pageID = 0xB01;}
            break; // 已经匹配到的立即结束匹配
        }
    }

    for (u8 i = 0; i < 20; i++) {
        if (!SwtichList[i])
            continue;
        else if (TouthX > SwitchTrigList[i][0] && TouthX < SwitchTrigList[i][2] && TouthY > SwitchTrigList[i][1] && TouthY < SwitchTrigList[i][3]) {
            activeID = SwtichList[i];
            break; // 已经匹配到就立即结束匹配
        }
    }
    LCD_ShowNum(60, 780, pageID, 4, 16);
    LCD_ShowNum(100, 780, activeID, 4, 16);
    if ( (_signed & 0x02))
        return;      // 没有触摸结果或本次触摸已经触发的不予响应
    _signed |= 0x02; // 本次触发生效
    // if (!pageID && !activeID)
    //     return;

    if (pageID) {
       
        EventNum  = 0; // 归零控件计数,为了页面绘制准备
        EventTell = 0; // 清除定时刷新标志位，该方法将在5.0弃用
        // UI_PageNum = pageID;
        pagenow = pageID;
        switch (pageID) {
  
            case 0xB01:
                UI_Page_Start();
                break;
            case 0xB10:
                UI_Page_AddNew();
                break;
            case 0xB20:
                UI_Page_DataSheet();
                break;
            case 0xB30:
                UI_Page_WifiSetting();
                break;
            case 0xB40:
                UI_Page_DeviceList();
                break;
            // case 0xB50:
            //     //UI_Page_Orther();
            //     break;
            case 0xB51:
                UI_Page_LineSetting();
                break;
            case 0xB52:
                UI_Page_LCD_pic();
                break;
            case 0xB53:
                UI_Page_SystemInfo();
                break;
            case 0xB50:
                default:
                // for (u8 i = 0; i < 20; i++) // 如果指向了意外的页面,交还事件列表
                //     EventList[i] = temp[i];
                // BACK_COLOR = RED;
                // LCD_ShowString(100, 780, 100, 18, 16, "unKnown Page!");
                 UI_Page_Start();
            break;
        }
    }
    else
    {
        switch (pagenow) {
  
            case 0xB01:
                UI_Page_Start();
                break;
            case 0xB10:
                UI_Page_AddNew();
                break;
            case 0xB20:
                UI_Page_DataSheet();
                break;
            case 0xB30:
                UI_Page_WifiSetting();
                break;
            case 0xB40:
                UI_Page_DeviceList();
                break;
            // case 0xB50:
            //     //UI_Page_Orther();
            //     break;
            case 0xB51:
                UI_Page_LineSetting();
                break;
            case 0xB52:
                UI_Page_LCD_pic();
                break;
            case 0xB53:
                UI_Page_SystemInfo();
                break;
            case 0xB50:
                default:
                // for (u8 i = 0; i < 20; i++) // 如果指向了意外的页面,交还事件列表
                //     EventList[i] = temp[i];
                // BACK_COLOR = RED;
                // LCD_ShowString(100, 780, 100, 18, 16, "unKnown Page!");
                 UI_Page_Start();
            break;
    }
    pageID = 0;
    if (activeID)
    {UI_Handle_Active(activeID);return;}
    activeID = 0;
    //callfunction(pageID, 0);
    _signed = 0;
}}

/// @brief 定时刷新部分区域
/// 切换页面将会抹除已挂起的标志，且不会因错误而交还
/// @attention 需要先挂起对应标识
void UI_TimeUpdate()
{

    if (!EventTell)
        return;
    // 5.0后 该段switch弃用
    switch (EventTell) {
        case 0xB20:
            UI_Active_DataSheet();
            break;
        case 0xB40:
            UI_Active_DeviceList();
            break;
        default:
            break;
    }
}
